
#include "mqtt_prop_entry.h"
#include "iota_payload.h"
#include  "bmsinfo.h"
#include "cJSON.h"
#include "hw_type.h"
#include "iota_payload.h"
#include "drv_dido.h"

// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------

/**
 * @brief
 *
 * @param item
 * @return true
 * @return false
 */
uint32_t CheckTopicItemList(char* itemStr)
{
    St_ListIotKey* p_listkey = getmListKey();
    uint32_t i = 0;
    for (i = 0; i < getSizeOfListKey(); i++)
    {
        if (p_listkey[i].key == NULL)   break;
        if (memcmp(p_listkey[i].key, itemStr, strlen(itemStr)) == 0)
        {
            DebugMsg("\r[long] Match item : %d %s  size:%d\n", i, itemStr, strlen(itemStr));
            return i;
        }
    }
    return 0xFFFF;
}


/**
 * @brief
 *
 * @param root
 */
void TopicDataProcess(cJSON *root)
{
//    _SystemDataStru *bmsInf = getBmsInf();
    St_ListIotKey* p_listkey = getmListKey();

    if (!cJSON_IsObject(root))
    {
        return;
    }
    cJSON *objectServ = cJSON_GetObjectItem(root, "services");
    size_t sizeArray = cJSON_GetArraySize(objectServ);
    DebugMsg("\r[long] size of Arry = %d\n", sizeArray);

    if (sizeArray <= 0)  return;

    // ---------------------------------------- analysis ----------------------------------------
    {
        // 只用到第一个数组，后面数组数据丢掉
        cJSON* objItem0 = cJSON_GetArrayItem(objectServ, 0);
        cJSON* ObjServId = cJSON_GetObjectItem(objItem0, "service_id");

        if(cJSON_IsString(ObjServId))
        {
            DebugMsg("\r[long] get TopicDataProcess : %s\n", ObjServId->valuestring);
            if (strcmp(ObjServId->valuestring, "solo_service") == 0) // 收到数据为server id ：solo_service
            {
                cJSON* ObjProperty = cJSON_GetObjectItem(objItem0, "properties");
                for (uint32_t i = 0; i < cJSON_GetArraySize(ObjProperty); i++)
                {
                    cJSON* item = cJSON_GetArrayItem(ObjProperty, i);
                    {
                        if (!cJSON_IsNull(item))
                        {
                            uint32_t index = CheckTopicItemList(item->string);
                            DebugMsg("\r[long] item : %s -> %s index=%d\n", item->string, item->valuestring, index);

                            if (index < getSizeOfListKey())
                            {
                                switch (p_listkey[index].type)
                                {
                                case UINT32_DATA:
                                    {
                                        uint32_t* p_u32value = p_listkey[index].p_value;
                                        *p_u32value = (uint32_t)atoi(item->valuestring);
                                        // DebugMsg("\r[long] Match UINT32_DATA : %d  str:%s \n", *p_u32value, item->valuestring);
                                    }
                                    break;
                                case INT32_DATA:
                                    {
                                        int32_t* p_i32value = p_listkey[index].p_value;
                                        *p_i32value = atoi(item->valuestring);
                                        // DebugMsg("\r[long] Match UINT32_DATA : %d  str:%s \n", *p_i32value, item->valuestring);
                                    }
                                    break;
                                case UINT16_DATA:
                                    {
                                        uint16_t* p_u16value = p_listkey[index].p_value;
                                        *p_u16value = atoi(item->valuestring);
                                        DebugMsg("\r[long] Match UINT16_DATA point: %d  str:%s (%d)\n", p_u16value, item->valuestring, *p_u16value);
                                    }
                                    break;
                                case INT16_DATA:
                                    {
                                        int16_t* p_i16value = p_listkey[index].p_value;
                                        *p_i16value = atoi(item->valuestring);
                                        DebugMsg("\r[long] Match INT16_DATA point: %d  str:%s (%d)\n", p_i16value, item->valuestring, *p_i16value);
                                    }
                                    break;
                                case UINT8_DATA:
                                    {
                                        uint8_t* p_u8value = p_listkey[index].p_value;
                                        *p_u8value = atoi(item->valuestring);
                                    }
                                    break;
                                case INT8_DATA:
                                    {
                                        int8_t* p_i8value = p_listkey[index].p_value;
                                        *p_i8value = atoi(item->valuestring);
                                    }
                                    break;
                                case FLOAT_DATA:
                                    {
                                        float* p_fvalue = p_listkey[index].p_value;
                                        *p_fvalue = atoi(item->valuestring);
                                    }
                                    break;
                                case U8_I8_DATA:
                                default:
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

/**
 *
 * */
void ProcessMessageData(char *payload, uint32_t payload_len)
{
    // DebugMsg("\r[long] ProcessMessageData payload len = %d\n", payload_len);
    cJSON *root = cJSON_Parse(payload);
    TopicDataProcess(root);
    cJSON_Delete(root);

    dev_dido_updateoutput();
}



